<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            background: url(images/2.gif);
        }

        .bigbox {
            width: 1700px;
            height: 800px;
            display: flex;
            margin: auto;
            margin-top: 70px;
        }

        .bigbox .left {
            width: 1280px;
            height: 800px;
            background-color: white;
            display: flex;
            flex-wrap: wrap;
            justify-content: space-around;
        }

        .bigbox .left .span1 {
            box-sizing: border-box;
            display: inline-block;
            width: 300px;
            height: 300px;
            margin-bottom: 95px;
            text-align: center;
            margin-top: 5px;
        }

        .bigbox .left .span1 .span4 {
            position: relative;
        }

        .bigbox .left .span1 .span4>img {
            position: absolute;
            left: 0;
            width: 100%;
        }

        .bigbox .left .span1 h4 {
            margin-top: 5px;
        }

        .bigbox .left .span1 .btn1 {
            width: 150px;
            height: 30px;
            background-color: rgb(0, 0, 0);
            border: 0;
            border-radius: 15%;
            color: rgb(255, 255, 255);
            margin-top: 5px;
        }

        .bigbox .right {
            flex: 1;
            background-color: white;
            overflow: auto;
        }

        .bigbox .right .p1 {
            font-size: 20px;
            text-align: center;
            margin-top: 22px;
            font-weight: 700;
        }

        .bigbox .right .span2 {
            height: 130px;
            width: 420px;
            margin-top: 10px;
            display: flex;
            flex-wrap: wrap;
            text-align: center;
            box-sizing: border-box;
        }

        .bigbox .right .span2 .inp1 {
            width: 13px;
        }

        .bigbox .right .span2 .span3 {
            width: 100px;
            height: 100px;
            margin-left: 10px;
            margin-bottom: 30px;
        }

        .bigbox .right .span2 .span3>img {
            width: 100%;
        }

        .bigbox .right .span2 .span3 .p2 {
            font-size: 16px;
            text-align: center;
        }

        .bigbox .right .span2 .price {
            width: 90px;
            background-color: white;
            line-height: 130px;
        }

        .bigbox .right .span2 .num {
            width: 100px;
            background-color: white;
            line-height: 130px;
        }

        .bigbox .right .span2 .delete {
            width: 107px;
            background-color: white;
            line-height: 130px;
        }
    </style>
</head>

<body>
    <div class="bigbox">
        <div class="left"></div>
        <div class="right">
            <p class="p1">购物车(0)</p>
            <div class="span2"></div>
        </div>
    </div>
</body>

</html>
<script src="./01.js"></script>
<script>
    //商品数据渲染
    function render() {
        var leftEle = document.querySelector('.left');
        leftEle.innerHTML = goodlist.map((item) => {
            return `
            <div class="span1">
                <img src = "${item.picture}" alt="">
                <h4>${item.name}</h4>
                <h4>${item.price}</h4>
                <button class="btn1">${item.btn}</button>
            </div>
            `
        }).join('')
    }
    render()


    //购物车数据
    var oo = { list: [] }
    console.log(oo.list);


    //渲染购物车列表
    function renderGouwuche() {
        var span2Ele = document.querySelector('.span2');
        span2Ele.innerHTML = oo.list.map((item) => {
            return `<input type="checkbox" class="inp1">
                    <span class="span3">
                        <img src="${item.picture}" alt="">
                        <p class="p2">${item.name}</p>
                    </span>
                    <div class="price">${item.price}</div>
                    <div class="num">${item.count}</div>
                    <div class="delete">删除收藏</div>`
        }).join('')
    }


    // 给left绑定点击事件
    var leftEle = document.querySelector('.left');
    leftEle.onclick = function (e) {
        if (e.target.tagName == 'BUTTON') {
            //获取点击商品的下标
            var btns = [...document.querySelectorAll('.btn1')]
            var index = btns.findIndex((item) => {
                return item == e.target
            })
            //添加商品数据到 购物车数组
            oo.list.push(goodlist[index])
            //重新渲染购物车列表
            renderGouwuche()
        }
    }

    //渲染商品数量
    function num() {
        var count = goodlist.reduce((t, item) => {
            return t + item.count
        }, 0)
        document.querySelector('.p1').innerHTML = `购物车(${count})`
    }


</script>